var app = getApp();
var url = app.url;
var pageNumber = 1;
Page({
  data: {
    showLoading: true, // 等待动画
    menu: '',
    chidx: 0,// 选中地区下标
    opType: '',// 1==开，0==关
    list:[],
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    
  },
  onShow: function () {
    // 页面显示
    this.getUserMeters()
    this.checkLogin()
  },
  onReady: function () {
    // 页面渲染完成
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  onPullDownRefresh: function () {  
    this.getUserMeters()
  },
  checkLogin: function () {
    var name = wx.getStorageSync('userName')
    if (name == '' || name == null) {
      wx.showModal({
        title: '提示',
        content: "用户未登录",
        confirmText: '去登录',
        showCancel: false,
        success: function () {
          wx.navigateTo({
            url: '../login/login',
          })
        }
      })
    }
  },
  getUserMeters: function () {
    var that = this;
    var list = this.data.list;
    var header = { 'content-type': 'application/x-www-form-urlencoded' }
    var params = { userName: wx.getStorageSync('userName'), pageSize: 20, pageNumber: pageNumber, }
    var encodingStr = app.ecbEncrypto(params);
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: url + '/web/exceptionReport/getWxList.htm',
      data: encodingStr,
      method: 'POST',
      header: header,
      success: function (res) {
        var decodingStr = app.ecbDecrypto(res.data)
        var rest = JSON.parse(decodingStr);
        rest.result.content.forEach(function(item){
          item.createTime = js_date_time(item.createTime);
        })
        if (rest.isSuccess == 0) {
          
          // for (var i in rest.result.content){
          //   list.push(rest.result.content[i])
          // }

          list = list.concat(rest.result.content);

          that.setData({ list: list })
          
        } else {
          wx.showModal({
            title: '提示',
            content: rest.errorMsg,
            showCancel: false
          })
        }
        console.log(rest)
      },
      complete: function () {
        wx.stopPullDownRefresh()
        that.setData({ showLoading: false })
        wx.hideLoading();
      }
    })
  },
  // 跳转
  showSzByName: function (e) {
    var idx = e.currentTarget.dataset.idx
    var list = this.data.fzList
    wx.setStorageSync("area" + idx, list[idx])// 缓存数据
    wx.navigateTo({
      url: '../control/zdarea?idx=' + idx,
    })
  },
  callPhone:function (e){
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone, 
      success: function () {
        
      },
      fail: function () {
        
      }
    });
  },
  bindToDetail: function (e) {
    var id = e.currentTarget.dataset.id;
  
    
    wx.navigateTo({
      url: 'reportDetail?id=' + id,
    })
  },
  tolower:function(){
    pageNumber++;
    this.getUserMeters();
  }
});


function js_date_time(unixtime) {
  var dateTime = new Date(parseInt(unixtime))
  var year = dateTime.getFullYear();
  var month = (dateTime.getMonth() + 1) < 10 ? "0" + (dateTime.getMonth() + 1) : (dateTime.getMonth() + 1);
  var day = dateTime.getDate() < 10 ? "0" + dateTime.getDate() : dateTime.getDate();
  var hour = dateTime.getHours() < 10 ? "0" + dateTime.getHours() : dateTime.getHours();
  var minute = dateTime.getMinutes() < 10 ? "0" + dateTime.getMinutes() : dateTime.getMinutes();
  var second = dateTime.getSeconds();
  var now = new Date();
  var now_new = Date.parse(now.toDateString());  //typescript转换写法
  var milliseconds = now_new - dateTime;
  var timeSpanStr = year + '-' + month + '-' + day + ' ' + hour + ':' + minute;
  return timeSpanStr;
}